package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author xin Zhou
 * @since 2024-01-10
 */
@Api(tags = "医院设置信息")
@Slf4j
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

//    @Autowired
//    private  HospitalSet hospitalSet;

//    查询所有医院设置
    @GetMapping("/findAll")
    @ApiOperation("查询所有医院设置信息")
    public R findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("items",list);
    }

    /*
    * @ApiParam(name = "id",value = "医院设置id",required = true)
    *   name 表示的是对应的列 value 表示的是备注信息 required 表示的是是否必须需要
    * */
//  添加删除方法
    @DeleteMapping("/deletedById/{id}")
    @ApiOperation("根据医院设置id删除医院信息")
    public R deletedById(@PathVariable("id") Integer Id){
        hospitalSetService.removeById(Id);
        return R.ok();
    }

    /*
      @Api(tags=""):标记在接口上
      @ApiOperation(value = ""): 标记在方法上
      @ApiParam(value ): 标记在参数上

      @ApiModel(description="") 对POJO类做说明
      @ApiModelProperty(value="") 对POJO类属性做说明
    * */

//    @RequestBody注解可以令前端发送的json数据转换为POJO对角
    @ApiOperation("带条件查询的分页")
    @PostMapping("/page/{pageNum}/{size}")
    public R getPageInfo(@ApiParam(name = "pageNum",value = "当前页",required = true) @PathVariable Long pageNum,
                         @ApiParam(name = "size",value = "每页显示多少个",required = true) @PathVariable Long size,
                         @RequestBody HospitalQueryVo hospitalQueryVo){
//        创建分页
        Page<HospitalSet> page = new Page<>(pageNum,size);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(hospitalQueryVo.getHosname())){
            queryWrapper.like("hosname",hospitalQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hospitalQueryVo.getHoscode())){
            queryWrapper.eq("hoscode",hospitalQueryVo.getHoscode());
        }

        hospitalSetService.page(page, queryWrapper);
        return R.ok().data("total",page.getTotal()).data("rows",page.getRecords());
    }

//    修改数据-->回显 --> 相当于 getById
    @ApiOperation("根据Id查找数据")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Integer id){
        return R.ok().data("item",hospitalSetService.getById(id));
    }

    @ApiOperation("更新数据")
    @PutMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

//    新增接口
    @ApiOperation("医院新增")
    @PostMapping("save")
    public  R save(@RequestBody HospitalSet hospitalSet){
//        设置状态1使用 0不能使用
        hospitalSet.setStatus(1);
//        签名密钥
        Random random = new Random();
//        System.currentTimeMillis()  获取时间戳
        String key = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(key);
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

//    批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("/delete")
    public R batchDelete(@RequestBody List<Integer> Ids){
        hospitalSetService.removeByIds(Ids);
        return R.ok();
    }

//    锁定与解锁
    @ApiOperation("更改是否显示(锁定与解锁)")
    @PutMapping("/status/{id}/{status}")
    public R updateStatus(@PathVariable("id") Long Id,@PathVariable Integer status){
//        利用Mybatis-plus的特性进行修改,进行传参的修改,如果没传参就不进行修改
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(Id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
}

